package com.cooper.dao.account;

import java.util.List;

import com.cooper.entity.SendAddressEntity;
import com.cooper.pagination.PageInfo;
import com.cooper.vo.AddressSearchVo;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.Insert;
import net.paoding.rose.jade.annotation.Paged;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.Update;

@DAO
public interface SendAddressDAO {
	
	static final String TABLE_NAME = "d_send_address";
	
	@ReturnGeneratedKeys
	@Insert(tableName=TABLE_NAME)
	Long saveEntity(SendAddressEntity entity) throws Exception;

	@Update(tableName=TABLE_NAME, keyColumn="id")
	boolean updateEntity(SendAddressEntity entity) throws Exception;
	
	@SQL("update d_send_address set deleted='y',modifier=:1.modifier,gmt_modified=:1.gmtModified where id=:1.id ")
	boolean deleteEntity(SendAddressEntity entity) throws Exception;
	
	@SQL("select * from d_send_address t where t.deleted='n' and id=:1 ")
	SendAddressEntity loadById(long addressId) throws Exception;
	
	@SQL("select * from d_send_address t where t.deleted='n' and origin=:1 and outer_id=:2 ")
	List<SendAddressEntity> findByOriginAndOuterId(String origin, long outerId) throws Exception;
	
	@SQL("update d_send_address set is_default=0 where deleted='n' and origin=:1 and outer_id=:2 and id<>:3")
	void releaseDefaultOther(String origin, Long userId, Long addressId) throws Exception;
	
	@SQL("update d_send_address set is_default=0 where deleted='n' and origin=:1 and outer_id=:2 and id=:3")
	void releaseDefaultThis(String origin, Long userId, Long addressId) throws Exception;
	
	@SQL("update d_send_address set is_default=1 where deleted='n' and id=:1 ")
	boolean setAsDefault(Long addressId) throws Exception;
	
	@SQL("select count(*) from d_send_address where deleted='n' and origin=:1.origin and outer_id=:1.outerId ")
	int queryAddressCount(AddressSearchVo searchVo) throws Exception;
	
	@Paged(paramIndex=1)
	@SQL("select * from d_send_address where deleted='n' and origin=:1.origin and outer_id=:1.outerId order by is_default desc,gmt_modified desc ")
	List<SendAddressEntity> queryAddressList(AddressSearchVo searchVo, PageInfo pageInfo) throws Exception;
}
